package com.study.modules.mini.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.study.common.BusinessException;
import com.study.modules.mini.repository.dao.WxUserDao;
import com.study.modules.mini.repository.model.WxUserDO;
import com.study.modules.mini.service.WxUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Author hupengming
 * @ClassName WxUserServiceImpl
 * @Date 2022-12-01  14:31
 * @Version 1.0
 */
@Service
public class WxUserServiceImpl implements WxUserService {

    @Autowired
    WxUserDao wxUserDao;

    @Override
    public WxUserDO getWxUserByOpenId(String openId) {
        return wxUserDao.selectUserByOpenId(openId);
    }

    @Override
    public WxUserDO getWxUserByUuid(String uuid) {
        return wxUserDao.selectOne(new LambdaQueryWrapper<WxUserDO>().eq(WxUserDO::getUuid,uuid));
    }

    @Override
    public void createUser(WxUserDO wxUserDO) {
        boolean flag = wxUserDao.insert(wxUserDO) > 0;
        if(!flag){
            throw new BusinessException("创建用户失败");
        }
    }

    @Override
    public void updateUserLastLoginDate(String uuid) {
        WxUserDO wxUserDO = this.getWxUserByUuid(uuid);
        wxUserDO.setLastLoginDate(new Date());
        wxUserDao.updateById(wxUserDO);
    }
}
